/*
 * @Author: zhouwr 1551541860@qq.com
 * @Date: 2024-03-02 22:49:17
 * @LastEditors: zhouwr 1551541860@qq.com
 * @LastEditTime: 2024-03-04 15:36:53
 * @FilePath: /jeecgboot-vue3/src/views/activiti/model/components/bpmn/Bpmn.api.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// import { BpmnContext } from './../../../../../../types/bpmn.d';

import { Ref } from "vue";
import { BpmnContext } from "/#/bpmn";
import Modeler from "bpmn-js/lib/Modeler";
import BpmnModeler from 'bpmn-js/lib/Modeler';
import { BaseViewerOptions } from "bpmn-js/lib/BaseViewer";

export interface ModdleElement {
  id: string;
  $type: string;
  value?: [ModdleElement];
  $attrs: { [key: string]: any };

  [key: string]: any;
}

export const BpmnStore: BpmnContext = {
  // modeler: undefined,
  initBpmnModeler: function (options?: BaseViewerOptions | undefined): void {
    this.modeler = new BpmnModeler(options);

    const elementRegistry = this.modeler.get('elementRegistry');
    //添加click、shapeAdd事件，属性更新后的事件，用于变更当前的业务对象，刷新属性配置栏
    ['element.click', 'shape.added'].forEach((event) => {
      this.addEventListener(event, function (elementAction) {
        // console.warn('elementAction', elementAction);
      });
    });
  },

  // getModeler: function () {
  //   return this.modeler;
  // },
  addEventListener: function (string, func): void {
    this.modeler?.on(string, function (e: any) {
      func(e);
    });
  },

  importXML(xml) {
    const scope = this;
    return new Promise((resolve, reject) => {
      if (scope.modeler) {
        scope.modeler.importXML(xml).then(result => {
          resolve(result);
          scope.modeler?.get('canvas').zoom('fit-viewport', 'auto');
        });
      } else {
        reject('BpmnModeler对象尚未定义！');
      }
    });
  },

  createDefaultXML(): void {
    try {
      const result = this.modeler?.createDiagram();
      const { warnings } = result;
      if (warnings?.length > 0) {
        console.log(warnings);
      } else {
        this.modeler?.get('canvas').zoom('fit-viewport', 'auto');
      }
    } catch (err: any) {
      console.log(err.message, err.warnings);
    }
  },
  getModeler: function (): typeof Modeler {
    return this.modeler
  }
}
